package cn.jtfadmin.core.modules.sys.domain.entity;

import cn.jtfadmin.base.data.jpa.domain.entity.BaseEntity;
import cn.jtfadmin.base.dict.annotations.Dict;
import cn.jtfadmin.base.lang.common.domain.entity.BuildInAbleEntity;
import cn.jtfadmin.base.lang.common.domain.entity.NamedTreeEntity;
import cn.jtfadmin.base.lang.enums.BooleanEnum;
import cn.jtfadmin.core.constant.OrgTypeConstant;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.comparator.Comparators;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

@Entity
@Table(name = "sys_depart")
@Getter
@Setter
@ToString(callSuper = true, exclude = {"parent", "childrenList"})
public class SysDepartEntity extends BaseEntity implements NamedTreeEntity, Comparable<SysDepartEntity>, BuildInAbleEntity {


    /**
     * 是否内置部门，内置部门不可删除
     */
    @Column(name = "build_in_flag", updatable = false)
    protected BooleanEnum buildInFlag;


    /**
     * 机构编号
     */
    @Column(name = "no",nullable = false, unique = true)
    @NotBlank
    @Size(max = 200)
    protected String no;

    /**
     * 部门名称
     */
    @Column(name = "name", length = 100, nullable = false)
    @NotBlank
    @Size(max = 100)
    protected String name;


    /**
     * 部门排序
     */
    @Column(name = "sort", nullable = false)
    @NotNull
    protected Integer sort;


    /**
     * 联系电话
     */
    @Basic
    @Column(name = "mobile", length = 20)
    @Size(max = 20)
    protected String mobile;

    /**
     * 传真
     */
    @Basic
    @Column(name = "fax", length = 200)
    @Size(max = 200)
    protected String fax;

    /**
     * 地址
     */
    @Basic
    @Column(name = "address", length = 200)
    @Size(max = 200)
    protected String address;

    /**
     * 备注
     */
    @Basic
    @Column(name = "memo", length = 200)
    @Size(max = 200)
    protected String memo;


    /**
     * 机构类型
     */
    @Column(name = "org_type", nullable = false)
    @NotNull
    @Dict(value = OrgTypeConstant.ORG_TYPE_DICT_CODE)
    protected Integer orgType;

    /**
     * 上级部门
     */
    @Column(name = "parent_id")
    protected String parentId;

    /**
     * 父类id 集合',pid,pid,'
     */
    @Column(name = "parent_ids")
    protected String parentIds;



    /**
     * 部门用户
     */
    @OneToMany()
    @JoinTable(name = "sys_user_depart", joinColumns = @JoinColumn(name = "sys_depart_id"),
            inverseJoinColumns = @JoinColumn(name = "sys_user_id"))
    protected List<SysUserEntity> sysUserList;


    /**
     * 部门负责人
     */
    @ManyToMany()
    @JoinTable(name = "sys_depart_leader_user", joinColumns = @JoinColumn(name = "sys_depart_id"),
        inverseJoinColumns = @JoinColumn(name = "sys_user_id"))
    protected List<SysUserEntity> leaderUsers;


    @Override
    public int compareTo(SysDepartEntity o) {
        return Comparators.nullsHigh().compare(this.sort, o.sort);
    }
}
